package com.changgou.order.service.impl;

import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SkuFeign skuFeign;
    @Autowired
    private SpuFeign spuFeign;

    //购物车redis key的前缀
    private static final String CART = "cart_";

    /***
     * 添加购物车
     * @param skuId
     * @param num
     * @param username
     */
    @Override
    public void addCart(String skuId, Integer num, String username) {
        //1.查一下redis里有这个人的购物车信息吗
        OrderItem orderItem = (OrderItem) redisTemplate.boundHashOps(CART + username).get(skuId);

        //2.购物车如果有这个orderItem,修改数据
        if (orderItem != null) {
            orderItem.setNum(orderItem.getNum() + num);//改变数量
            if (orderItem.getNum() <= 0) {
                //从redis里删除这个商品
                redisTemplate.boundHashOps(CART + username).delete(skuId);
                return;
            }
            orderItem.setMoney(orderItem.getPrice() * orderItem.getNum());//这一项的总价
            orderItem.setPayMoney(orderItem.getPrice() * orderItem.getNum());//这一项的支付总价
        } else {
            //3.购物车没有这个orderItem,构建orderItem
            Sku sku = skuFeign.findById(skuId).getData();
            Spu spu = spuFeign.findSpuById(sku.getSpuId()).getData();
            orderItem = this.sku2OrderItem(sku, spu, num);
        }
        //4.存入redis
        redisTemplate.boundHashOps(CART + username).put(skuId, orderItem);
    }

    /***
     * 查询购物车列表数据
     * @param username
     * @return
     */
    @Override
    public Map list(String username) {

        Map resultMap = new HashMap();

        //1.从redis里查这个人的购物车里的信息
        List<OrderItem> orderItemList = redisTemplate.boundHashOps(CART + username).values();
        resultMap.put("orderItemList", orderItemList);

        //2.总数量和总价
        Integer totalNum = 0;
        Integer totalMoney = 0;
        Integer totalWeight = 0;

        for (OrderItem orderItem : orderItemList) {
            totalNum += orderItem.getNum();
            totalMoney += orderItem.getMoney();
            totalWeight += orderItem.getWeight();
        }
        resultMap.put("totalNum", totalNum);
        resultMap.put("totalMoney", totalMoney);
        resultMap.put("totalWeight", totalWeight);

        return resultMap;
    }

    private OrderItem sku2OrderItem(Sku sku, Spu spu, Integer num) {

        OrderItem orderItem = new OrderItem();

        orderItem.setCategoryId1(spu.getCategory1Id());
        orderItem.setCategoryId2(spu.getCategory2Id());
        orderItem.setCategoryId3(spu.getCategory3Id());
        orderItem.setSpuId(sku.getSpuId());
        orderItem.setSkuId(sku.getId());
        orderItem.setName(sku.getName());
        orderItem.setPrice(sku.getPrice());
        orderItem.setNum(num);
        orderItem.setMoney(sku.getPrice() * num);
        orderItem.setPayMoney(sku.getPrice() * num);
        orderItem.setImage(sku.getImage());
        orderItem.setWeight(sku.getWeight() * num);

        return orderItem;
    }
}
